//
//  ViewController.m
//  UITest
//
//  Created by yao zhaofeng on 2018/6/5.
//  Copyright © 2018年 YaoZhaofeng. All rights reserved.
//

#import "ViewController.h"
#import "XYTextField.h"
#import "RNLoadingView.h"
#import "XYView.h"
#import "NSString+Encode.h"
#import <CoreText/CTFontManager.h>
#import "UIDevice+SafeArea.h"

@interface ViewController ()
@property(nonatomic, strong) UIWebView *webView;
@property(nonatomic, strong) UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIImageView *img1;
@property (weak, nonatomic) IBOutlet UIImageView *img2;
@end
int num = 0;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
    _webView.delegate = self;
//    [self.view addSubview:_webView];
//    NSURL *url = [NSURL URLWithString:@"https://support.jj.cn/survey_react/index.html?page=MobLobby&surveyID=30442&net=wifi&operator=Telcom&hideback=true&apkVer=51010&sdkVer=4522&NotchH=0&NavigationH=0&loginState=2&device=iphone&showbplan=false&theme=12&theme_mode=2&isNewHall=true&time=1625141168353"];
//    NSURL *url = [NSURL URLWithString:@"https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2018103061934387&biz_content=%7B%22certify%5Fid%22%3A%222107873c5e42dbfb4dfbdadfe1fec3ef%22%7D&charset=utf-8&method=alipay.user.certify.open.certify&sign_type=RSA2&timestamp=2021-11-02 11:16:20&version=1.0&sign=Dm5HXVgQ3vi%2FPnYxqzd8OYyDGO9rw3p2F6nsHe6ocDe1iqLnxbj%2FRFOto1IuGbznL%2FUn7Ns9yU3UcW6L19wO7tL6QPQaXeS5vPVZUpgKyiPn8Im36Y%2F2qv%2F%2BVCLK3BccXi3nKrBNIGfQvtJfCcr2kb2L1W3iDvd8cmDEa0loIz%2B3CbfXY6oFHMoIq0%2FhOdG9HYqojGx%2FxsBdh1qtX82%2FrpEthIqQZkZXfeJSlv9TFWcUh8E8vKLRStJGODJZwB%2FwgCy3mZ2P4jWfSIrWCGOFfU9sjSeNdo6eOwZIrBk8BdNjuonkb71yURwshuiLox8u2KUL5nK%2F55hB%2FefwpAyywg%3D%3D"];
    NSURL *url = [NSURL URLWithString:@"https://www.jj.cn"];
//    NSURL *url = [NSURL URLWithString:@"https://openapi.alipay.com/gateway.do?alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2015111100758155&biz_content=%7B%22certify_id%22%3A%22ZM201611253000000121200404215172%22%7D&charset=GBK&format=json&method=alipay.user.certify.open.certify&sign=MhtfosO8AKbwctDgfGitzLvhbcvi%2FMv3iBES7fRnIXn%2BHcdwq9UWltTs6mEvjk2UoHdLoFrvcSJipiE3sL8kdJMd51t87vcwPCfk7BA5KPwa4%2B1IYzYaK6WwbqOoQB%2FqiJVfni602HiE%2BZAomW7WA3Tjhjy3D%2B9xrLFCipiroDQ%3D&sign_type=RSA2&timestamp=2016-11-25+15%3A00%3A59&version=1.0&sign=MhtfosO8AKbwctDgfGitzLvhbcvi%2FMv3iBES7fRnIXn%2BHcdwq9UWltTs6mEvjk2UoHdLoFrvcSJipiE3sL8kdJMd51t87vcwPCfk7BA5KPwa4%2B1IYzYaK6WwbqOoQB%2FqiJVfni602HiE%2BZAomW7WA3Tjhjy3D%2B9xrLFCipiroDQ%3D"];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
//    [urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    _webView.scalesPageToFit = YES;
    _webView.scrollView.bounces = NO;
    _webView.clipsToBounds = YES;
    
    [_webView loadRequest:urlRequest];
    
    _textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    _textView.text = @"你好，全世界";
    _textView.backgroundColor = [UIColor redColor];
    _textView.returnKeyType = UIReturnKeyContinue;
//    [_textView becomeFirstResponder];
    [self.view addSubview:_textView];

//    UIFont *font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:24];
//    _myLabel.font = font;

    UIFont *font = [self customFontWithPath:[[NSBundle mainBundle] pathForResource:@"FontNormal" ofType:@"ttf"] size:17];
//    font = [self customFontWithPath:[[NSBundle mainBundle] pathForResource:@"FontNormal_old" ofType:@"ttf"] size:17];
//    font = [self customFontWithPath:[[NSBundle mainBundle] pathForResource:@"SourceHanSansCN-VF-2" ofType:@"otf"] size:17];
//    _textView.font = font;
    
    _img1.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://webcloudplt.srv.jj.cn/0/upload/3025/cloudcmsExt/picture/ad720276ba3ca6a6_73223_240x240.png"]]];
    //
    
    NSLog(@"yaozhaofeng...xy_safeAreaTop = %lf", [UIDevice xy_safeAreaTop]);
    NSLog(@"yaozhaofeng...xy_safeAreaBottom = %lf", [UIDevice xy_safeAreaBottom]);
    NSLog(@"yaozhaofeng...xy_statusBarHeight = %lf", [UIDevice xy_statusBarHeight]);
    NSLog(@"yaozhaofeng...scale = %lf", [UIScreen mainScreen].scale);
    NSLog(@"yaozhaofeng...%@", NSStringFromCGRect([UIScreen mainScreen].nativeBounds));
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"%@", NSStringFromUIEdgeInsets(VIEWSAFEAREAINSETS(self.view)));
}


- (void)viewWillLayoutSubviews {
    NSLog(@"%@", NSStringFromUIEdgeInsets(VIEWSAFEAREAINSETS(self.view)));
    UIEdgeInsets edgeInsets = VIEWSAFEAREAINSETS(self.view);
//    [_webView setFrame:CGRectMake(0, edgeInsets.top, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - edgeInsets.top - edgeInsets.bottom)];
}
    
-(void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    NSLog(@"%@", NSStringFromUIEdgeInsets(VIEWSAFEAREAINSETS(self.view)));
}

- (void)printFont {
    NSArray*familyNames = [UIFont familyNames];
    for (NSString*familyName in familyNames){
        printf("yaozhaofeng...Family: %s \n", [familyName UTF8String]);
        NSArray*fontNames = [UIFont fontNamesForFamilyName:familyName];
        for(NSString*fontName in fontNames ){
            printf("\tyaozhaofeng...Font: %s \n", [fontName UTF8String] );
        }
    }
    
}

- (IBAction)buttonClick:(id)sender {
//    [RNLoadingView show];
    UIFont *font;
    if (num%3 == 0) {
        font = [self customFontWithPath:[[NSBundle mainBundle] pathForResource:@"FontNormal" ofType:@"ttf"] size:17];
    } else if (num%3 == 1) {
        font = [self customFontWithPath:[[NSBundle mainBundle] pathForResource:@"SourceHanSansCN-VF-2" ofType:@"otf"] size:17];
    } else if (num%3 == 2) {
        font = [self customFontWithPath:[[NSBundle mainBundle] pathForResource:@"FontNormal_old" ofType:@"ttf"] size:17];
    }
    _textView.font = font;
    num++;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (UIFont*)customFontWithPath:(NSString*)path size:(CGFloat)size
{
    NSURL *fontUrl = [NSURL fileURLWithPath:path];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithURL((CFURLRef)fontUrl);
    CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
    CGDataProviderRelease(fontDataProvider);
    CTFontManagerRegisterGraphicsFont(fontRef, NULL);
    NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
    UIFont *font = [UIFont fontWithName:fontName size:size];
    CGFontRelease(fontRef);
    return font;
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [_webView reload];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"request is %@", request);
    return true;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"webViewDidStartLoad:(UIWebView *)webView{");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"webViewDidFinishLoad");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"didFailLoadWithError error is %@", error);
}

@end
